package dao

import (
	"context"
	"sale-admin/internal/dal"
	"sale-admin/internal/define"
	"sale-admin/internal/models"

	"192.168.1.75/go-pkg/errorx"
)

type ActivityOrder struct{}

// Create 新增活动订单
func (ActivityOrder) Create(
	ctx context.Context,
	query *dal.Query,
	activityType int,
	activityID string,
	outTradeNO string,
	childOutTradeNo string,
	robotID string,
	merchantID string,
	locationID, provinceID, cityID, districtID string,
	goodsID string,
	goodsPrice, paymentAmount, discountAmount float64,
	paymentType, orderStatus int,
) error {
	ao := query.ActivityOrder
	ao.WithContext(ctx).Create(&models.ActivityOrder{
		ActivityType:    activityType,
		ActivityID:      activityID,
		OutTradeNo:      outTradeNO,
		ChildOutTradeNO: childOutTradeNo,
		RobotID:         robotID,
		MerchantID:      merchantID,
		LocationID:      locationID,
		ProvinceID:      provinceID,
		CityID:          cityID,
		DistrictID:      districtID,
		GoodsID:         goodsID,
		GoodsPrice:      goodsPrice,
		PaymentAmount:   paymentAmount,
		DiscountAmount:  discountAmount,
		PaymentType:     paymentType,
		OrderStatus:     orderStatus,
	})
	return nil
}

// UpdateStatus 更新订单状态
func (ActivityOrder) UpdateStatus(
	ctx context.Context,
	query *dal.Query,
	outTradeNO string,
	paymentType int, // 支付方式
	orderStatus int, // 订单状态
) error {
	ao := query.ActivityOrder
	res, _ := ao.WithContext(ctx).Where(ao.OutTradeNo.Eq(outTradeNO)).UpdateSimple(
		ao.PaymentType.Value(paymentType),
		ao.OrderStatus.Value(orderStatus),
	)
	if res.RowsAffected <= 0 {
		return errorx.New("更新失败", -1)
	}
	return nil
}

// List 活动订单明细
func (ActivityOrder) List(
	ctx context.Context,
	query *dal.Query,
	activityID string,
) {

}

type GoodsListType struct {
	GoodsID    string `gorm:"column:goods_id" json:"goods_id"`       // 商品id
	GoodsPrice string `gorm:"column:goods_price" json:"goods_price"` // 商品价格
	GoodsName  string `gorm:"-" json:"goods_name"`                   // 商品名称
	GoodsImage string `gorm:"-" json:"goods_image"`                  // 商品图片
	GoodsType  int    `gorm:"-" json:"goods_type"`                   // 商品类型
	TotalNum   string `gorm:"column:total_num" json:"total_num"`     // 销量统计
	PaidNum    string `gorm:"column:paid_num" json:"paid_num"`       // 支付统计
}

// GoodsList 活动订单的商品列表
func (ActivityOrder) GoodsList(
	ctx context.Context,
	query *dal.Query,
	activityOrderType define.ActivityOrderType, // 活动类型
	activityID string, // 活动id
	goodsIDs []string,
	orderStatus define.StatusType, // 订单状态
	page, limit int,
) (records []GoodsListType, total int64) {
	ao := query.ActivityOrder
	tx := ao.WithContext(ctx).
		Where(ao.ActivityType.Eq(int(activityOrderType))).
		Where(ao.ActivityID.Eq(activityID)).
		Where(ao.OrderStatus.Eq(int(orderStatus))).
		Group(ao.GoodsID)
	if len(goodsIDs) > 0 {
		tx = tx.Where(ao.GoodsID.In(goodsIDs...))
	}
	if page > 0 {
		tx = tx.Offset((page - 1) * limit)
	}
	if limit > 0 {
		tx = tx.Limit(limit)
	}
	total, _ = tx.Count()
	tx.Select(
		ao.GoodsID,                         // 商品id
		ao.GoodsPrice,                      // 商品价格
		ao.GoodsID.Count().As("total_num"), // 付款总件数
		ao.OutTradeNo.Distinct().Count().As("paid_num"), // 付款总人数
	).Scan(&records)
	if len(records) == 0 {
		records = []GoodsListType{}
	}
	return
}
